//
//  CTPublishImageViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/5/15.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPublishImageViewController.h"
#import "CTTabBarController.h"

//header
#import "CTHeader.h"
#import "CTAppInitFile.h"

//view
#import "CTPublishImageView.h"

//logic
#import "CTPublishLogic.h"


//func
#import "CTDebugManager.h"
#import "CTShare.h"
#import "CTRemindView.h"
#import "CTAppLanguage.h"
#import "CTDeviceFunction.h"
#import "CTAppInfo.h"
#import "CTImageProcessing.h"
#import "CTGenRequestData.h"
#import "CTActivitorView.h"
#import "CTTextManager.h"
#import "MobClick.h"


//model
#import "CTShareItem.h"
#import "CTDiscoveryItems.h"

@interface CTPublishImageViewController ()<CTPublishImageViewDelegate>
{
    CTPublishImageView * publishView;
    CTPublishLogic * publishLogic;
    
    CTActivitorView * activitorView;
    
    NSString * publishTextStr;
}
@end

@implementation CTPublishImageViewController

@synthesize editImage,groupAry,topicGroupStr,waterImage;

- (void)dealloc
{
    [activitorView removeFromSuperview];
    activitorView = nil;
    
    [publishView removeFromSuperview];
    publishView = nil;
    
    publishLogic = nil;
    publishTextStr = nil;
    
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:CT_NOTI_PUBLISH_SUCCESS object:nil];
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPublishImageViewController Is Removed!!!"];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        publishLogic = [[CTPublishLogic alloc] init];
        
//        //注册发布内容通知
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(publishSuccess:) name:CT_NOTI_PUBLISH_SUCCESS object:nil];
        
        
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //统计
    [MobClick beginLogPageView:@"CTPublishImageViewController"];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //统计
    [MobClick endLogPageView:@"CTPublishImageViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    publishView = [[CTPublishImageView alloc] init];
    publishView.delegate = self;
    [self.view addSubview:publishView];
    
    publishView.publishImage = editImage;
    
    CTFollowList * followListItem = [[CTFollowList alloc] init];
    followListItem.topic_list = groupAry;
    followListItem.text = @"";
    publishView.followListItem = followListItem;
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark - request  -

-(void)sendPublishRequest
{
    
    //判断是否同步到新浪微博
    if ([CTShare isSinaShare] == YES) {
        CTShareItem * shareItem = [[CTShareItem alloc] init];
        shareItem.img = waterImage;
        shareItem.content = @"PicoPico";
        shareItem.defaultContent = @"PicoPico";
        shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];;
        shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
        
        [CTShare sinaBtnAction:shareItem shareSuccessHandler:^{
            
        } shareFailedHandler:^{
            
        }];
    }
    

 
    
    
     activitorView = [[CTActivitorView alloc] init];
    [activitorView showActivitorViewOn:self.view withType:CTActivitorViewLongShadow];
    UIImage *image = [CTImageProcessing imageCompressionImg:editImage WithSize:CGSizeMake(780, 780)];

    [publishLogic publishRequest:[CTGenRequestData genRequestPublishUpadteItem:image content:publishTextStr withGroup:topicGroupStr] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [activitorView removeActivitorView];
            activitorView = nil;
      
            
          [self performSelector:@selector(publishSuccess:) withObject:nil afterDelay:1];
            
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [activitorView removeActivitorView];
            activitorView = nil;
            
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}

-(NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:detailDic options:0 error:nil];
//    NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


#pragma mark - publish view delegate -

//返回
- (void)publishImageViewBackBtnAction:(CTPublishImageView *) view
{
    [self.navigationController popViewControllerAnimated:YES];
}
//发送
- (void)publishImageViewSendBtnAction:(CTPublishImageView *)view
{
    [self sendPublishRequest];
}
//是否同步到微博
- (void)publishImageViewSynchronizationBtnAction:(CTPublishImageView *)view withPublishBool:(BOOL) publishBool
{
    
}
//发送附带文字
- (void)publishImageViewPublishTextView:(CTPublishImageView *)view withStr:(NSString *)publishStr
{
//    if (publishTextStr  ) {
//        <#statements#>
//    }
    publishTextStr = publishStr;
}

/***********************************分享*********************************/

//分享到微信朋友圈
- (void)publishImageViewShareToWechatFriends:(CTPublishImageView *) view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.img = waterImage;
    shareItem.content = @"";
    shareItem.descr = @"";
    shareItem.content = @"PicoPico";
    shareItem.defaultContent = @"";
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_topic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechatFriend publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ空间
- (void)publishImageViewShareToQQSpace:(CTPublishImageView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    CGSize size = CGSizeMake(320, 320);
    shareItem.img = [CTImageProcessing imageCompressionImg:waterImage WithSize:size];
    shareItem.content = publishTextStr;
    shareItem.defaultContent = publishTextStr;
    shareItem.title = publishTextStr;
    shareItem.url = @"http://picopico.yuchats.com";
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQSpace publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到新浪微博
- (void)publishImageViewShareToSina:(CTPublishImageView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.img = waterImage;
    shareItem.content = @"PicoPico";
    shareItem.defaultContent = @"PicoPico";
    shareItem.descr = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];;
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare sinaBtnAction:shareItem shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });

    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });

    }];
    
//    [CTShare shareItem:shareItem shareChannel:CTShareChannelSina publishType:CTPublishTypeImage shareSuccessHandler:^{
//        dispatch_async(dispatch_get_main_queue(), ^{
//            CTRemindView * remindView = [[CTRemindView alloc] init];
//            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
//        });
//    } shareFailedHandler:^{
//        dispatch_async(dispatch_get_main_queue(), ^{
//            CTRemindView * remindView = [[CTRemindView alloc] init];
//            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
//        });
//    }];
}
//分享到微信
- (void)publishImageViewShareToWeChat:(CTPublishImageView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    shareItem.img = waterImage;
    shareItem.descr = publishTextStr;
    shareItem.defaultContent = publishTextStr;
    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];
    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
    
    [CTShare shareItem:shareItem shareChannel:CTShareChannelWechat publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//分享到QQ
- (void)publishImageViewShareToQQ:(CTPublishImageView *)view withImage:(UIImage *)image
{
    CTShareItem * shareItem = [[CTShareItem alloc] init];
    CGSize size = CGSizeMake(320, 320);
    shareItem.img = [CTImageProcessing imageCompressionImg:waterImage WithSize:size];
    shareItem.descr = publishTextStr;
    shareItem.defaultContent = publishTextStr;
//    shareItem.content = [CTAppLanguage localizedStringForKey:@"share_pic_describe" withValue:nil withTable:@"Language"];
//    shareItem.title = [CTAppLanguage localizedStringForKey:@"share_title" withValue:nil withTable:@"Language"];
   
    [CTShare shareItem:shareItem shareChannel:CTShareChannelQQ publishType:CTPublishTypeImage shareSuccessHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_successed" withValue:nil withTable:@"Language"]];
        });
    } shareFailedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_Publish_failed" withValue:nil withTable:@"Language"]];
        });
    }];
}
//保存到本地
- (void)publishImageViewShareToSave:(CTPublishImageView *)view withImage:(UIImage *)image
{
    [CTDeviceFunction saveImageToAlbum:[CTAppInfo appName] withImage:image];
    
    CTRemindView * remindView = [[CTRemindView alloc] init];
    [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_saveLocal_successed" withValue:nil withTable:@"Language"]];
}




#pragma mark - notification -

-(void)publishSuccess:(id)sender
{
    CTTabBarController * tabbarVC = [CTAppInitFile rootTabbar];
    [tabbarVC selectTabbarIndex:1];
    
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:CT_NOTI_PUBLISH_SUCCESS object:nil];
    }];
}


@end
